using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The ParentStatusCodeTableDB class is responsible for interacting with the database to retrieve and store information
    /// about ParentStatusCodeTable objects.
    /// </summary>
    public class ParentStatusCodeTableDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of ParentStatusCodeTable from the underlying datasource.
        /// </summary>
        /// <param name="parentStatusCode">The unique ParentStatusCode of the ParentStatusCodeTable in the database.</param>
        /// <returns>An ParentStatusCodeTable when the ParentStatusCode was found in the database, or null otherwise.</returns>
        public static ParentStatusCodeTable GetItem(short parentStatusCode)
        {
            ParentStatusCodeTable myParentStatusCodeTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocParentStatusCodeTableSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@parentStatusCode", parentStatusCode);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myParentStatusCodeTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myParentStatusCodeTable;
            }
        }

        /// <summary>
        /// Returns a list with ParentStatusCodeTable objects.
        /// </summary>
        /// <returns>A generics List with the ParentStatusCodeTable objects.</returns>
        public static ParentStatusCodeTableList GetList()
        {
            ParentStatusCodeTableList tempList = new ParentStatusCodeTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocParentStatusCodeTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Saves a ParentStatusCodeTable in the database.
        /// </summary>
        /// <param name="myParentStatusCodeTable">The ParentStatusCodeTable instance to save.</param>
        /// <returns>The new ParentStatusCode if the ParentStatusCodeTable is new in the database or the existing ParentStatusCode when an item was updated.</returns>
        public static int Save(ParentStatusCodeTable myParentStatusCodeTable)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocParentStatusCodeTableInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (myParentStatusCodeTable.ParentStatusCode == -1)
                    {
                        myCommand.Parameters.AddWithValue("@parentStatusCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@parentStatusCode", myParentStatusCodeTable.ParentStatusCode);
                    }
                    if (string.IsNullOrEmpty(myParentStatusCodeTable.Description))
                    {
                        myCommand.Parameters.AddWithValue("@description", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@description", myParentStatusCodeTable.Description);
                    }
                    if (myParentStatusCodeTable.Status == null)
                    {
                        myCommand.Parameters.AddWithValue("@status", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@status", myParentStatusCodeTable.Status);
                    }
                    if (myParentStatusCodeTable.CreatedDateTime == null)
                    {
                        myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@createdDateTime", myParentStatusCodeTable.CreatedDateTime);
                    }
                    if (myParentStatusCodeTable.ModifiedDateTime == null)
                    {
                        myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@modifiedDateTime", myParentStatusCodeTable.ModifiedDateTime);
                    }

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt32(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a ParentStatusCodeTable from the database.
        /// </summary>
        /// <param name="parentStatusCode">The ParentStatusCode of the ParentStatusCodeTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(short parentStatusCode)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocParentStatusCodeTableDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@parentStatusCode", parentStatusCode);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the ParentStatusCodeTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static ParentStatusCodeTable FillDataRecord(IDataRecord myDataRecord)
{
ParentStatusCodeTable myParentStatusCodeTable = new ParentStatusCodeTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ParentStatusCode")))
{
myParentStatusCodeTable.ParentStatusCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("ParentStatusCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Description")))
{
myParentStatusCodeTable.Description = myDataRecord.GetString(myDataRecord.GetOrdinal("Description"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myParentStatusCodeTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myParentStatusCodeTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myParentStatusCodeTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myParentStatusCodeTable;
}
    }

}
